package Schema::Result::Project;

use strict;
use warnings;
use utf8;

use base qw/DBIx::Class::Core/;

__PACKAGE__->table('Project');
__PACKAGE__->add_columns( 
    Id => { data_type => 'integer', is_auto_increment => 1 }, 
    CompanyId  => {},
    Name  => {},
    Comment  => {},
    AggregatePackage  => {},
    EnumPackage  => {},
    LanguageType  => {},
);
__PACKAGE__->set_primary_key('Id');
__PACKAGE__->belongs_to(Company => 'Schema::Result::Company', 'CompanyId');
__PACKAGE__->has_many(Enums => 'Schema::Result::CustomEnum', 'ProjectId', { cascade_delete => 1 });
__PACKAGE__->has_many(Configurations => 'Schema::Result::Configuration', 'ProjectId', { cascade_delete => 1 });
__PACKAGE__->has_many(Aggregates => 'Schema::Result::Aggregate', 'ProjectId', { cascade_delete => 1 });
__PACKAGE__->has_many(Identity => 'Schema::Result::Identity', 'ProjectId', { cascade_delete => 1 });

# 创建项目
sub Create{
    my($class, $schema, $params) = @_;

    my $newProject = $schema->resultset('Project')->create($params);

    return $newProject;
}

# 修改项目信息
sub Modify{
    my ($self, $params) = @_;
    if(defined $params->{Name}){
        $self->Name($params->{Name});
    }
    if(defined $params->{AggregatePackage}){
        $self->AggregatePackage($params->{AggregatePackage});
    }
    if(defined $params->{EnumPackage}){
        $self->EnumPackage($params->{EnumPackage});
    }
    if(defined $params->{Comment}){
        $self->Comment($params->{Comment});
    }
    if(defined $params->{LanguageType}){
        $self->LanguageType($params->{LanguageType});
    }

    $self->update;
}

# 新增配置
sub AddConfiguration{
    my ($self, $key, $value, $comment) = @_;
    my $newConfiguration = $self->create_related(
        'Configurations',
        {
            Key => $key,
            Value => $value,
            Comment => $comment,
        }
    );

    return $newConfiguration;
}

# 修改项目配置
sub ModifyConfiguration{
    my ($self, $configurationId, $key, $value, $comment) = @_;

    # 0 表示失败 1 表示成功
    my $result = 0;
    # 失败原因
    my $reason = '';

    # 获取即将修改的配置
    my $toModifyConfiguration = $self->Configurations()->find($configurationId);


    unless (defined $toModifyConfiguration){
        $reason = "Configuration with ID $configurationId not found for this project.";
        return {Success => $result, Reason => $reason};
    }

    eval {
        $toModifyConfiguration->update({
                Key => $key,
                Value => $value,
                Comment => $comment,
            });
        $result = 1;
    };

    if($@){
        $reason = "Failed to update configuration: $@";
        $result = 0;
    }

    return { Success => $result, Reason => $reason };
}

# 删除项目配置
sub DeleteConfiguration{
    my ($self, $configurationId) = @_;

    # 0 表示失败 1 表示成功
    my $result = 0;
    # 失败原因
    my $reason = '';

    # 获取即将修改的配置
    my $toModifyConfiguration = $self->Configurations()->find($configurationId);

    unless (defined $toModifyConfiguration){
        $reason = "Configuration with ID $configurationId not found for this project.";
        return {Success => $result, Reason => $reason};
    }

    eval {
        $toModifyConfiguration->delete;
        $result = 1;
    };

    if($@){
        $reason = "Failed to update configuration: $@";
        $result = 0;
    }

    return { Success => $result, Reason => $reason };
}

# 删除
sub Delete {
    my ($self) = @_;

    $self->delete;
}

1;
